//
//  UI.m
//  EFExtend
//
//  Created by mac on 15/3/8.
//  Copyright (c) 2015年 Eric. All rights reserved.
//

#import "UI.h"

@interface DemoCell : UITableViewCell

@property (nonatomic,strong) UILabel *titleLbel;
@property (nonatomic,strong) UILabel *detailLbel;

@end

@implementation DemoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        if (self.titleLbel == nil) {
            self.titleLbel = (id)[UIView viewAsLabel];
            [self.titleLbel EFText:@"test"];
            [self.contentView addSubview:self.titleLbel];
            [self.titleLbel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.contentView.mas_top).offset(0);
                make.left.mas_equalTo(self.contentView.mas_left).offset(0);
                make.right.mas_equalTo(self.contentView.mas_right).offset(0);
            }];
        }
        if (self.detailLbel == nil) {
            self.detailLbel = (id)[UIView viewAsLabel];
            self.detailLbel.numberOfLines = 0;
            [self.detailLbel EFText:@"testtesttesttesttesttesttetesttesttesttesttesttesttesttesttest"];
            [self.contentView addSubview:self.detailLbel];
            [self.detailLbel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.titleLbel.mas_bottom).offset(5);
                make.left.mas_equalTo(self.contentView.mas_left).offset(0);
                make.right.mas_equalTo(self.contentView.mas_right).offset(0);
                make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-5);
            }];
        }
    }
    return self;
}

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize{
    targetSize = [super systemLayoutSizeFittingSize:targetSize];
    return targetSize;
}

- (void)dealloc{
    self.detailLbel = nil;
    self.titleLbel = nil;
    HM_SUPER_DEALLOC();
}

@end


@interface UI ()<UISearchBarDelegate,UISearchDisplayDelegate>
@property (nonatomic, HM_STRONG) UISearchBar *mySearchBar;
@property (nonatomic, HM_STRONG) UISearchDisplayController *mySearchDisplayController;
@end

@implementation UI{
    NSMutableArray *boards;
}

- (void)dealloc
{
    self.tableView = nil;
    HM_SUPER_DEALLOC();
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    NSLog(@"UI---viewWillTransitionToSize");
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    NSLog(@"UI---willAnimateRotationToInterfaceOrientation");
}
- (void)load{
    self.includesOpaque = YES;
//    self.allowedLandscape = YES;
//    self.allowedPortrait = NO;
}
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
//    return UIInterfaceOrientationLandscapeRight;
//}
//- (BOOL) shouldAutorotateToInterfaceOrientation:
//(UIInterfaceOrientation)toInterfaceOrientation {
//    return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
//}
//- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    return UIInterfaceOrientationMaskLandscapeRight;
//}

- (BOOL)shouldAutorotate{
    return YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self setNavigationBarTitleColor:[UIColor whiteColor]];
    
    [self setNavigationBarBackgroundColor:[[UIColor redColor] colorWithAlphaComponent:.3f]];
//    [self setNavigationBarOriginalColor:[[UIColor redColor] colorWithAlphaComponent:.3f]];
    [self setNavigationBarTranslucent:YES];
    
    INFO(self.ttttt);
    
    [self.customNavLeftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
//    [self.customNavLeftBtn setTitle:@"🏠" forState:UIControlStateNormal];
    [self.customNavLeftBtn setFrame:CGRectMakeBound(32, 32)];
    
    boards = [[NSMutableArray alloc]init];
    [boards addObject:@{@"title":@"页面切换",@"subtitle":@"各种切换方式",@"map":URLFOR_controller(@"transition")}];
    [boards addObject:@{@"title":@"按钮",@"subtitle":@"各种按钮方式",@"map":URLFOR_storyboard(@"Main.Button")}];
    [boards addObject:@{@"title":@"弹出框",@"subtitle":@"提示、弹出",@"map":URLFOR_controller(@"popover")}];
    [boards addObject:@{@"title":@"颜色",@"subtitle":@"主题色、颜色工具",@"map":URLFOR_controller(@"color")}];
    [boards addObject:@{@"title":@"字体",@"subtitle":@"自定义字体",@"map":URLFOR_controller(@"font")}];
    [boards addObject:@{@"title":@"图片浏览",@"subtitle":@"多种图片浏览",@"map":URLFOR_controller(@"imageBrowse")}];
    [boards addObject:@{@"title":@"标签条",@"subtitle":@"tabbar",@"map":URLFOR_controller(@"tabbar")}];
    [boards addObject:@{@"title":@"侧滑",@"subtitle":@"slide",@"way":@"open",@"map":URLFOR_controllerWithNav(@"slide")}];
    [boards addObject:@{@"title":@"进度条",@"subtitle":@"progress",@"map":URLFOR_controller(@"progress")}];
    [boards addObject:@{@"title":@"图像绘制",@"subtitle":@"context",@"map":URLFOR_controller(@"contextSample")}];
    [boards addObject:@{@"title":@"自动布局",@"subtitle":@"AutolayoutBoard",@"map":URLFOR_controller(@"AutolayoutBoard")}];
    [boards addObject:@{@"title":@"键盘相关",@"subtitle":@"Keyboard",@"map":URLFOR_controller(@"Keyboard")}];
    [boards addObject:@{@"title":@"cellection视图",@"subtitle":@"context",@"map":URLFOR_controller(@"cellectionView")}];
    [boards addObject:@{@"title":@"地图",@"subtitle":@"context",@"map":URLFOR_controller(@"map")}];
    [boards addObject:@{@"title":@"图像",@"subtitle":@"context",@"map":URLFOR_controller(@"imagePicker")}];
    [boards addObject:@{@"title":@"城市选",@"subtitle":@"context",@"map":URLFOR_controller(@"CitySelecter")}];
    [boards addObject:@{@"title":@"二维码",@"subtitle":@"context",@"map":URLFOR_controller(@"RCode")}];
    [boards addObject:@{@"title":@"MJ下拉刷新",@"subtitle":@"context",@"map":URLFOR_controller(@"MJExampleViewController")}];
    [self.tableView registerClass:[DemoCell class] forCellReuseIdentifier:@"listCell"];
    
    [self.tableView reloadData];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

ON_Button(signal){
    UIButten *btn = signal.source;
    if ([signal is:[UIButten TOUCH_UP_INSIDE]]) {
        if ([btn is:@"leftBtn"]) {
//            self.tableView.delegate = nil;
//            self.tableView.dataSource = nil;
//            self.tableView = nil;
            [self backAndRemoveWithAnimate:YES];
//            [self backWithAnimate:YES];
        }
    }
}



#pragma  mark - table delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return boards.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier=@"listCell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        
    }
    NSDictionary *city = [boards objectAtIndex:indexPath.row];
    cell.textLabel.text = [city valueForKey:@"title"];
    cell.detailTextLabel.text = [city valueForKey:@"subtitle"];
    cell.detailTextLabel.textColor = RGB(.5, .5, .5);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *city = [boards objectAtIndex:indexPath.row];
    NSString *map = [city valueForKey:@"map"];
    NSString *title = [city valueForKey:@"title"];
    NSString *way = [city valueForKey:@"way"];
    
    UIViewController *c = nil;
    if ([way is:@"open"]) {
        c = [self open:map name:nil animate:YES mapItem:^(HMURLMAPItem *mapItem) {
        }];
    }else{
        c =[self pushToPath:map animated:YES];
    }
    if ([c isKindOfClass:[UINavigationController class]]) {
        [(UINavigationController*)c topViewController].title = title;
    }else{
        c.title = title;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //autolayout 动态计算Cell高度
    
    CGFloat height = [tableView fd_heightForCellWithIdentifier:@"listCell" cacheByIndexPath:indexPath configuration:^(UITableViewCell* cell) {
    }];
    
    return height;
}
@end
